<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script type="text/javascript" src="../js/template-web.js"></script>
<script type="text/javascript" src="../js/jquery-1.12.4.js"></script>
<script type="text/html" id="classesListTemplate">
    {{each classesList classes index}}
    {{if index==0}}
        <option value="0" selected="selected">--请选择分类--</option>
        <option  value="{{classes.id}}">{{classes.cname}}</option>
    {{else}}
        <option value="{{classes.id}}">{{classes.cname}}</option>
    {{/if}}
    {{/each}}
</script>
<script type="text/javascript">
    $(function () {
        $.post("../faqs/getAllClasses",function (data) {
            var classesListhtml = template("classesListTemplate",{classesList:data});
            $("#classes").html(classesListhtml)
        })
    })
    function back() {
        window.location = "index.html";
    }
    function save() {
        var classId = $("#classes").val();
        var title = $("input[id=title]").val();
        var content = $("#content").val();
        if($("#classes").val()==0){
            alert("请选择分类");
            return;
        }
        if($("input[id=title]").val()==''){
            alert("请输入标题");
            return;
        }
        if($("#content").val()==''){
            alert("请输入内容");
            return;
        }
        $.post("../faqs/add",{classId:classId,title:title,content:content},function (data) {
            if(data.code==0){
                alert("添加成功");
                window.location = "index.html";
            }else if (data.code==1){
                alert("标题重复，请重新输入");
            }
            else{
                alert("系统忙，请稍后再试");
                window.location = "index.html";
            }
        })
    }

</script>
<body>
<h2 style="text-align: center">添加常见问题</h2>
<center>
    <table>
        <tbody>
        <tr>
            <td>分类：</td>
            <td>
                <select id="classes">

                </select>
            </td>
        </tr>
        <tr>
            <td>标题：</td>
            <td>
                <input id="title" type="text">
            </td>
        </tr>
        <tr>
            <td>内容：</td>
            <td>
                <textarea id="content"></textarea>
            </td>
        </tr>
        <tr>
            <td>
                <button id="save" onclick="save()">保存</button>
            </td>
            <td>
                <button id="back" onclick="back()">取消</button>
            </td>
        </tr>
        </tbody>
    </table>
</center>
</body>
</html>